package com.mall.shopping.services;


import com.github.pagehelper.PageHelper;
import com.mall.shopping.IHomeService;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentDto;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = IHomeService.class)
@Component
public class IHomeServiceImpl implements IHomeService {
    //转换器
    @Autowired
    ContentConverter contentConverter;

    //Mapper
    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper contentMapper;

    @Autowired
    ItemMapper itemMapper;


    @Override
    public HomePageResponse homepage() {

        //1、首先创建一个返回值VO
        HomePageResponse homePageResponse = new HomePageResponse();


        //2、先查panel,注意显示在主页的位置positon=0，并且要根据sortOrder排序(大概不需要排序吧，但是我想知道为什么)

        //这个不需要分页，因为首页什么 都会显示
        Example example = new Example(Panel.class);
        example.setOrderByClause("sort_order" + " " + "asc");
        example.createCriteria().andEqualTo("position",0);
        List<Panel> panelList = panelMapper.selectByExample(example);


        //3、创建一个返回的set,用来给homePageResp返回参数
        Set<PanelDto> result = new HashSet() {
        };


        //4、遍历panel并且查出子类
        for (Panel panel : panelList) {
            //首先转成dto
            PanelDto panelDto = contentConverter.panen2Dto(panel);

            //得到Dto的id，用以查询子类;得到limitNum用来显示几个
            int id = panelDto.getId();
            int num = panelDto.getLimitNum();


            //首先分页,永远显示第一页，且每页数量就是要显示的数量
            PageHelper.startPage(1,num);


            //这里开始搜出排序后的数据
            Example example1 = new Example(PanelContent.class);
            example1.setOrderByClause("sort_order" + " " + "asc");
            example1.createCriteria().andEqualTo("panelId",id);
            List<PanelContent> panelContents = contentMapper.selectByExample(example1);
            List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);


            List<PanelContentItem> list = new ArrayList<>();

            //遍历该子队列，并且得到productId从item表中查数据填入
            for (PanelContentDto panelContentDto : panelContentDtos) {

                //首先new一个panelContentItem将之前的数据放进去

                PanelContentItem panelContentItem = new PanelContentItem();
                panelContentItem.setId(panelContentDto.getId());
                panelContentItem.setPanelId(panelContentDto.getPanelId());
                panelContentItem.setType(panelContentDto.getType());
                panelContentItem.setProductId(panelContentDto.getProductId());
                panelContentItem.setSortOrder(panelContentDto.getSortOrder());
                panelContentItem.setFullUrl(panelContentDto.getFullUrl());
                panelContentItem.setPicUrl(panelContentDto.getPicUrl());
                panelContentItem.setPicUrl2(panelContentDto.getPicUrl2());
                panelContentItem.setPicUrl3(panelContentDto.getPicUrl3());


                //通过商品id去数据库中查数据
                //如果为空则不进行下面的了

                Long productId = panelContentDto.getProductId();
                if(productId != null) {
                    Item item = itemMapper.selectByPrimaryKey(productId);

                    //将数据给最后的那几个属性赋值
                    panelContentItem.setCreated(item.getCreated());
                    panelContentItem.setUpdated(item.getUpdated());
                    panelContentItem.setProductName(item.getTitle());
                    panelContentItem.setSalePrice(item.getPrice());
                    panelContentItem.setSubTitle(item.getSellPoint());
                }

                list.add(panelContentItem);

            }

            //将list变成Dto的list
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(list);


            //赋给该panelDto
            panelDto.setPanelContentItems(panelContentItemDtos);


            //将该panelDto赋值给Set
            result.add(panelDto);


        }

        homePageResponse.setPanelContentItemDtos(result);



        return homePageResponse;
    }
}
